package com.dlwx.parttimetalent.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*自我介绍、工作经历*/
public class IntroduceActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.et_content)
    EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        int type = getIntent().getIntExtra("type", -1);
        String introduce = (String) SPUtils.get(IntroduceActivity.this, Constant.INTRODUCE, "");
        etContent.setText(introduce);
        switch (type) {
            case Constant.ZERO:
                tvMiddleTitle.setText("自我介绍");
                break;
//            case Constant.ONE:
//                tvMiddleTitle.setText("工作经历");
//                break;
        }
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String content=s.toString();
                if(content.length()>499){
                    ToastUtils.toastForShort(IntroduceActivity.this,"字数限定在500字以内");
                }
            }
        });

    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
                String content=etContent.getText().toString();
                if(TextUtils.isEmpty(content)){
                    ToastUtils.toastForShort(this,"内容不能为空");return;
                }
                confirm(content);
                break;
        }
    }

    private void confirm(final String introduce) {
        loadingDialog.show();
        OkgoUtil.post(Http.setuUserInfo, IntroduceActivity.this)
                .params("infoName", "selfinfo")
                .params("values", introduce)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(IntroduceActivity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            SPUtils.put(IntroduceActivity.this,Constant.INTRODUCE,introduce);
                            IntroduceActivity.this.finish();
                        } else {
                        }
                        ToastUtils.toastForShort(IntroduceActivity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }
}
